package com.fgs.common.widget.dialog;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;

import com.fgs.common.CommonDialog;
import com.fgs.common.R;
import com.fgs.common.entity.region.AreaInfo;
import com.fgs.common.entity.region.CityInfo;
import com.fgs.common.entity.region.ProvinceInfo;
import com.fgs.common.entity.region.RegionInfo;
import com.fgs.common.widget.PlaceSelectView;

/**
 * @author fgs
 * @date 2020/9/10
 * @description
 **/
public class CitySearchSelectDialog extends CommonDialog {

    private PlaceSelectView mPlaceSelectView;
    private OnCitySearchSelectListener onCitySearchSelectListener;

    private RegionInfo regionInfo;

    public CitySearchSelectDialog(@NonNull Context context) {
        super(context, R.style.style_select_dialog);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_city_search_select);


        Window window = this.getWindow();
        window.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
//        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = window.getWindowManager().getDefaultDisplay().getHeight() / 3 * 2;
        window.setAttributes(params);

        setCanceledOnTouchOutside(false);


        regionInfo = new RegionInfo();
        mPlaceSelectView = findViewById(R.id.dialog_city_search_select_placeSelectView);

        mPlaceSelectView.setOnPlaceConditionSelectListener(new PlaceSelectView.OnPlaceConditionSelectListener() {
            @Override
            public void onSelect(String province, String city, String area) {
                dismiss();
            }

            @Override
            public void onSelect(ProvinceInfo provinceInfo, CityInfo cityInfo, AreaInfo areaInfo) {
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.append(provinceInfo.name);
                if (areaInfo != null && !TextUtils.isEmpty(cityInfo.name)) {
                    stringBuilder.append(" ");
                    stringBuilder.append(cityInfo.name);
                    regionInfo.city = cityInfo.name;
                    regionInfo.code = cityInfo.code;
                }
                if (areaInfo != null && !TextUtils.isEmpty(areaInfo.name) && !areaInfo.name.equals("全市")) {
                    stringBuilder.append(" ");
                    stringBuilder.append(areaInfo.name);
                    regionInfo.county = areaInfo.name;
                    regionInfo.code = areaInfo.code;
                }

                regionInfo.label = stringBuilder.toString();
                regionInfo.province = provinceInfo.name;
                if (TextUtils.isEmpty(regionInfo.code)) {
                    regionInfo.code = provinceInfo.code;
                }

                if (TextUtils.isEmpty(regionInfo.county)) {
                    regionInfo.county = "";
                }

                regionInfo.parentcode = regionInfo.code;

                if (onCitySearchSelectListener != null) {
                    onCitySearchSelectListener.onCitySelect(regionInfo);
                }
                dismiss();
            }
        });
    }


    public interface OnCitySearchSelectListener {
        void onCitySelect(RegionInfo regionInfo);
    }

    public void setOnCitySearchSelectListener(OnCitySearchSelectListener onCitySearchSelectListener) {
        this.onCitySearchSelectListener = onCitySearchSelectListener;
    }
}
